package com.cmwa.solution.sys.permission.privilege.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.Result;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.sys.permission.dataAuthority.entity.RoleVo;
import com.cmwa.solution.sys.permission.dataRole.service.RoleService;
import com.cmwa.solution.sys.permission.privilege.entity.PrivilegeVo;
import com.cmwa.solution.sys.permission.privilege.service.PrivilegeService;


/**
 * 权限控制controller
 * @author zhougz
 * @date 2016年03月31日
 * 
 */
@Controller
@RequestMapping(value = "/privilege/privilegeUpdate")
public class PrivilegeController extends BaseController{
	@Resource
	private PrivilegeService privilegeService;
	@Resource
	private RoleService roleService;
	
	private final static String PAGE_PATH = "system/sys/permission/privilege/";
	
	@RequestMapping(value="/executeUpdateLimit",method={RequestMethod.GET})
	public void executeUpdateLimit(HttpServletResponse response) throws IOException{
	    privilegeService.executeUpdateLimit();
	    String status = "更新成功";
		PrintWriter out = response.getWriter();
		out.print("{\"STATUS\":\""+status+"\"}");
	}
	
	
	/**
	 *  权限设置页面
	 * @author ex-huangch 2016.5.24
	 * @return
	 */
	@RequestMapping("/privilegeListView")
	public String privilegeListView(ModelMap model) {
		return PAGE_PATH + "privilegeList";
	}
	
    /**
    * 权限设置分页数据
    * @author ex-huangch 2016.5.24
    * @param sp
    * @return
    */
    @RequestMapping("/privilegeListPage")
    @ResponseBody
    public Page<PrivilegeVo> privilegeListPage(SearchParam sp) {
		Page<PrivilegeVo> list = privilegeService.privilegeListPage(sp);
	     return list;
    }
    
    /**
     * 权限设置详情页面
     * @param roleId
     * @param model
     * @return
     * @author ex-zhangjia
     */
    @RequestMapping("/privilegeDetailView")
    public String privilegeDetailView(@RequestParam(value = "roleId") String roleId, ModelMap model) {
    	RoleVo roleVo = roleService.queryByRoleId(roleId);
    	List<RoleVo> roleShareInfo = roleService.queryShareInfoById(roleId);
    	model.addAttribute("roleVo",roleVo);
    	model.addAttribute("roleShareInfo",roleShareInfo);
    	
    	
    	
	    return PAGE_PATH + "privilegeDetail";
    }
    
    /**
     * 权限设置修改页面
     * @param roleId
     * @param model
     * @return
     * @author ex-zhangjia
     */
    @RequestMapping("/privilegeUpdateView")
    public String privilegeUpdateView(@RequestParam(value = "roleId") String roleId, ModelMap model) {
    	RoleVo roleVo = roleService.queryByRoleId(roleId);
    	//分享权限列表
    	List<RoleVo> roleShareInfo = roleService.queryShareInfoById(roleId);
    	//已选列表
//    	List<PrivilegeVo> resourceInfo = privilegeService.queryResourceInfo(roleId);
    	//可选列表
//    	List<PrivilegeVo> resourceForSel = privilegeService.queryResourceForSel(roleId,"");
    	model.addAttribute("roleVo",roleVo);
    	model.addAttribute("roleShareInfo",roleShareInfo);
//    	model.addAttribute("resourceInfo",resourceInfo);
//    	model.addAttribute("resourceForSel",resourceForSel); 
	     return PAGE_PATH + "privilegeUpdate";
    }
    
    /**
     * 修改页 资源名选择框列表
     * @param roleId
     * @return
     * @author ex-zhangjia
     */
    @RequestMapping("/queryResourceForSel")
    @ResponseBody
    public Page<PrivilegeVo> queryResourceForSel(SearchParam searchParam) {
    	Page<PrivilegeVo> resourceForSel = privilegeService.queryResourceForSel(searchParam);
	    return resourceForSel;
    }
    

    /**
     * 修改页 资源名选择框列表
     * @param roleId
     * @return
     * @author ex-zhangjia
     */
    @RequestMapping("/querySelectedResource")
    @ResponseBody
    public Page<PrivilegeVo> querySelectedResourceListPage(SearchParam searchParam) {
    	Page<PrivilegeVo> selectedResources = privilegeService.querySelectedResourceListPage(searchParam);
	    return selectedResources;
    }
    
    @RequestMapping("/addResource")
    @ResponseBody
    public Result<Object> addResource(@RequestParam("roleId")String roleId, @RequestParam("isAll")String isAll, @RequestParam("resIds")String resIds) {
    	Result<Object> result = new Result<Object>(true, "修改成功");
    	
    	try {
			privilegeService.addResource(roleId, isAll, resIds);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
    	
    	return result;
    }
    

    @RequestMapping("/removeResource")
    @ResponseBody
    public Result<Object> removeResource(@RequestParam("roleId")String roleId, @RequestParam("isAll")String isAll, @RequestParam("resIds")String resIds) {
    	Result<Object> result = new Result<Object>(true, "修改成功");
    	
    	try {
			privilegeService.removeResource(roleId, isAll, resIds);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
    	
    	return result;
    }
}
